﻿using System;
using RayDen.Library.Core;
using RayDen.RayEngine.Core.Types;
using RayDen.RayEngine.Data;
using RayDen.Library.Core.Primitives;

namespace PathEngine
{


    public class Ray : IRay
    {
        public const int InvalidId = int.MinValue;

        public int ParentId;
        public int Id;
        public RayData RayData;

        public RayDifferential Differential;

        protected Ray()
        {
            
        }

        public Point Origin
        {
            get { return RayData.Org; }
        }

        public Vector Direction
        {
            get { return RayData.Direction; }
        }

        public Vector InvDirection
        {
            get { return RayData.InvDirection; }
        }

        public float Min
        {
            get { return RayData.Min; }
        }

        public float Max
        {
            get { return RayData.maxT; }
        }

        public Point Point(float distance)
        {
            return RayData.Point(distance);
        }

        public void SetSegment(float min, float max)
        {
            RayData.SetSegment(min, max);
        }
    }

    public class RayDifferential : RayInfo
    {
        internal bool hasDifferentials;
        internal Point rxOrigin, ryOrigin;
        internal Vector rxDirection, ryDirection;
        internal float time;
        internal int depth;

        public RayDifferential(ref Point org, ref Vector dir, float t0 = 0.0001f, float t1 = 10000f)
        {
            this.Org = org;
            this.Dir = dir;
            this.MinT = t0;
            this.MaxT = t1;
            InvDir = 1f / dir;
            hasDifferentials = false;

        }
        public RayDifferential(RayInfo vray)
            : base(vray)
        {
            hasDifferentials = false;
        }

        public void ScaleDifferentials(float s)
        {
            rxOrigin    = Org + (rxOrigin - Org) * s;
            ryOrigin    = Org + (ryOrigin - Org) * s;
            rxDirection = Dir + (rxDirection - Dir) * s;
            ryDirection = Dir + (ryDirection - Dir) * s;
        } 
    }
}